var UserAdd = [];
var UserRight = "";
function dialogUtilisateur() {
  var a = $("#dialogUtilisateur");
  var btns = {};
  btns[BUTTON_OK] = function() {$(this).dialog("destroy")};
  a.dialog({autoOpen:false, closeOnEscape: false, draggable:false, modal:true, width:"auto", buttons:btns});
  a.dialog("open")
}
function getListDelUtilisateur() {
  $.post(baseUrl + "/connexion/getutilisateur/", {}, makeListDelUtilisateur, "json")
}
function makeListDelUtilisateur(a) {
  var b = [], c = $("#checkListUtilisateur");
  c.empty();
  for(var d = 0, e = a.list.length;d < e;d++) {
    b[d] = '<dt><input type=checkbox><span style="margin-left: 5px">' + a.list[d] + "</span><dt>"
  }
  $(c).append(b.join(""))
}
function getCheckListUtilisateur() {
  var a = "";
  $("#checkListUtilisateur input:checkbox[checked]").each(function() {
    a += this.nextSibling.textContent + ","
  });
  delListUtilisateur(a)
}
valid.add("getCheckListUtilisateur", getCheckListUtilisateur);
function delListUtilisateur(a) {
  var b = $("#checkListUtilisateur").find('input').length, c = a.split(',');
  c = c.length - 1;
  if ( b == 1 || b == c) {
          alertGrowl(MISC_ERROR, USER_ACCOUNT_PRESENT)
          return false
  }
  $.post(baseUrl + "/connexion/deluser", {list:a}, alertGrowl(USER_ACCOUNT_DEL_TXT, USER_ACCOUNT_DEL_SUCESS), "json")
}
function dialogDelUser() {
  getListDelUtilisateur();
  var a = $("#dialogUserList");
  var btns = {};
  btns[BUTTON_CANCEL] = function() {$(this).dialog("destroy")};
  btns[BUTTON_DEL] = function() {$(this).dialog("destroy");validWindows("getCheckListUtilisateur")};
  a.dialog({autoOpen:false, draggable:false, closeOnEscape: false, modal:true, width:200, buttons:btns});
  a.dialog("open")
}
function getListLang() {
  $.post(baseUrl + "/connexion/getlistlang/", {}, makeListLang, "json")
}
function makeListLang(a) {
  var c = $("#dialogAddUser"), e = $(c + "[name|=listLangage]"), f = [];
  e.empty();
  a = a.listLang.split(";");
  for(var b = 0, g = a.length;b < g;b++) {
    if($(e + "option[value=" + a[b] + "]").length == 0) {
      f[b] = "<option value='" + a[b] + "'>" + a[b] + "</option>"
    }
  }
  $(e).append(f.join(""))
}
function dialogAddUser() {
  getListLang();
  var a = $("#dialogAddUser"), b = $(a + "[name|=login]"), c = $(a + "[name|=email]"), d = $(a + "[name|=pass1]"), e = $(a + "[name|=pass2]");
  b.val('');
  c.val('');
  d.val('');
  e.val('');
  var btns = {};
  btns[BUTTON_CANCEL] = function() {$(this).dialog("destroy")};
  btns[BUTTON_OK] = function() {$(this).dialog("destroy");checkDialogAddUser(true)};
  a.dialog({autoOpen:false, draggable:false, modal:true, width:"auto", buttons:btns});
  a.dialog("open")
}
$(function() {
   var a = $("#boutonUtilisateur")
   $(a + "[b|=addNetwork]").click(addNetwork);
   $(a + "[b|=removeNetwork]").click(removeNetwork)
});
function addNetwork(a, b, c, d) {
  idNetwork++;
  if ( typeof a == 'undefined' || typeof b == 'undefined' || typeof c == 'undefined' )
      a = b = c = d ='';
  var e = '<input type="text" id="netName' + idNetwork + '" name="netName' + idNetwork + '" size="25" value="' + a + '"/>';
  e += '<input type="text" id="netLogin' + idNetwork + '" name="netLogin' + idNetwork + '" size="25" value="' + b + '" />';
  e += '<input type="text" id="netPass' + idNetwork + '" name="netPass' + idNetwork + '" size="25" value="' + c + '" />';
  e += '<input type="text" id="confPass' + idNetwork + '" name="confPass' + idNetwork + '" size="25" value="' + d + '" />';
  e += '<button type="button" class="boutonUtilisateur" id="boutonUtilisateur' + idNetwork + '" onclick="validWindows(\'delListNetwork\', \'' + a + '\')">' + MISC_DEL + '</button>';
  $("#networkListMenu").append(e);
  return true
}
function delListNetwork(a) {
 $.ajax({type:"POST", url:baseUrl + "/connexion/delnetwork/", success:alertGrowl(USER_NETWORK_TXT, USER_NETWORK_DEL), dataType:"json", async:false, data:({name:a})});
 drawListNetwork()
}
valid.add("delListNetwork", delListNetwork);
function removeNetwork() {
  $("#netName" + idNetwork).remove();
  $("#netLogin" + idNetwork).remove();
  $("#netPass" + idNetwork).remove();
  $("#confPass" + idNetwork).remove();
  $("#boutonUtilisateur" + idNetwork).remove();
  idNetwork--;
  return true
}
function drawListNetwork() {
  var b = $("#networkListMenu");
  b.find("*").remove()
  getListNetwork();
}
function dialogNetworkManager() {
  drawListNetwork()
  var a = $("#dialogNetworkManager");
  var btns = {};
  btns[BUTTON_CANCEL] = function() {$(this).dialog("destroy")};
  btns[BUTTON_OK] = function() {$(this).dialog("destroy");addListNetwork()};
  a.dialog({autoOpen:false, draggable:false, modal:true, width:'auto',position:[125,125], buttons:btns});
  a.dialog("open")
}
function addListNetwork() {
  var a = 0, b, c, d, e;
  if (idNetwork == 0)
          return false
  $("#networkListMenu :input").each(function() {
    a++;
    if(a == 1) {
      b = $(this).val();
      if(b.length == 0) {
        alertGrowl(MISC_ERROR, USER_NETWORK_EMPTY);
        return false
      }
    }
    if(a == 2) {
      c = $(this).val()
    }
    if(a == 3) {
      d = $(this).val();
    }
    if(a == 4) {
      e = $(this).val();
      updateListNetwork(b, c, d, e);
    }
    if(a == 5) {
      a = 0
    }
});
  alertGrowl(USER_NETWORK_TXT, USER_NETWORK_SUCCESS)
}
function updateListNetwork(a, b, c, d) {
  $.post(baseUrl + "/connexion/updatenetwork/", {name:a, login:b, pass:c, confpass:d}, null, "json")
}
function getListNetwork() {
  $.post(baseUrl + "/connexion/getnetwork/", {}, makeListNetwork, "json")
}
function makeListNetwork(a) {
  for(var d = 0, e = a.list.length;d < e;d++) {
    var b = a.list[d].PassNetwork.split(',')
    addNetwork(a.list[d].NameNetwork, a.list[d].LoginNetwork, b[0], b[1])
  }
  if ( e == 0 )
     addNetwork()
}
function getListDroit() {
    $.post(baseUrl + "/connexion/getlistdroits/", {}, makeListDroit, "json")
}
function makeListDroit(b) {
  var f = [], g = $("#checkDroitUtilisateur"), c = $("#listDroits"), h = [], i = $("#droitDesc");
  g.empty();c.empty();i.val('');
  $.each(b.list, function(k, i) {
    for(var j = "<b>" + k + " :</b>", e = 0, l = i.length;e < l;e++) {
      var t = i[e].split(';')
      j += '<dt><input type=checkbox d="' + t[1] + '"><span style="margin-left: 5px">' + t[0] + "</span><dt>"
    }
    j += '<br/>';
    f.push(j)
  });
  h[0] = "<option value='-'>-</option>";
  for(var a =1, d = b.description.length;a < d;a++) {
    if($(c + "option[value=" + b.description[a - 1] + "]").length == 0) {
      h[a] = "<option value='" + b.description[a - 1] + "'>" + b.description[a - 1] + "</option>"
    }
  }
  $(g).append(f.join(""));
  $(c).append(h.join(""))
  changeListDroit(c, g)
}
function changeListDroit(b, c) {
  $(b).change(function() {
    var a = $(this).val();
    if (a == '-') {
        $("#droitDesc").val("");
        c.find("*").attr("checked", false);
        return false;
    }
    $("#droitDesc").val(a);
    applyListDroit(a)
  })
}
function delListDroit() {
  if ($("#listDroits :selected").val() == '-') {
      return false;
  }
  $.ajax({type:"POST", url:baseUrl + "/connexion/deldescdroit/", success:alertGrowl(USER_RIGHT_TXT, USER_RIGHT_DEL), dataType:"json", async:false, data:({desc:$("#droitDesc").val()})});
   getListDroit()
}
valid.add("delListDroit", delListDroit);
function applyListDroit(a) {
 $.post(baseUrl + "/connexion/getinfodroit/", {desc:a}, applyListDroitResp, "json")
}
function getInfoDroitUser() {
  $.post(baseUrl + "/connexion/getinfodroitutilisateur/", {login:UserRight}, getInfoDroitUserResp, "json")
}
function getInfoDroitUserResp(a) {
  UserRight = "";
  $("#droitDesc").val(a.desc);
  if(a.list[0].length == "")
      return false
  applyListDroitResp(a)
}
function applyListDroitResp(a) {
  var c = $("#checkDroitUtilisateur");
  c.find("*").attr("checked", false);
  for(var b = 0, d = a.list.length;b < d;b++) {
    if (a.list[b] == "")
        continue
    $(c + "[d|=" + a.list[b] + "]").attr("checked", true)
  }
}
function dialogDroitUser(a, b, c, d, f, e) {
  UserAdd.t = a, UserAdd.l = b, UserAdd.e = c,  UserAdd.d = d,  UserAdd.p = e, UserAdd.i = f;
  getListDroit();
  if ( UserRight.length > 0 && a == false)
      getInfoDroitUser();
  var g = $("#dialogUserDroit");
  var btns = {};
  btns[BUTTON_CANCEL] = function() {$(this).dialog("destroy")};
  btns[BUTTON_OK] = function() {$(this).dialog("destroy");getCreateDroit();};
  g.dialog({autoOpen:false, draggable:false, closeOnEscape:false, modal:true, width:'auto', position:[475,100], buttons:btns});
  g.dialog("open")
}
function getCreateDroit() {
  var a = "", b;
  $("#checkDroitUtilisateur input:checkbox[checked]").each(function() {
    a += $(this).attr('d') + ";"
  });
  b = $("#droitDesc").val();
  if(a == '') {
    alertGrowl(MISC_ERROR, USER_RIGHT_EMPTYRIGHT);
    return false
  }
  if(b.length == 0) {
    alertGrowl(MISC_ERROR, USER_RIGHT_EMPTYTEMPLATE);
    return false
  }
   if(/^[\w\d\_\.\ ]{4,}$/.test(b) == false) {
    alertGrowl(MISC_ERROR, USER_RIGHT_BADTEMPLATE);
    return false
  }
  UserAdd.t ? addUserBase(UserAdd.l, UserAdd.e, b, UserAdd.i, UserAdd.p, a) : updateUserBase(UserAdd.l, UserAdd.e, b, UserAdd.i, UserAdd.p, a);
  UserAdd = [];
  return true
}
function checkDialogAddUser(e) {
  var c = $("#login"), b = $("#email"), a = $("#pass1"), d = $("#pass2"), f = $(".right:checked"), g = $("#listLangage :selected");
  if(c.val().length == 0 || b.val().length == 0) {
    alertGrowl(MISC_ERROR, USER_ADDUSER_EMPTYFIELD);
    return false
  }
  if(/^[\w\d\_\.]{4,}$/.test(c.val()) == false) {
    alertGrowl(MISC_ERROR, USER_ADDUSER_BADLOGIN);
    return false
  }
  if(/^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/.test(b.val()) == false) {
    alertGrowl(MISC_ERROR, USER_ADDUSER_BADMAIL);
    return false
  }
  if(e) {
    if(a.val() != d.val() || a.val().length == 0 || d.val().length == 0) {
      alertGrowl(MISC_ERROR, USER_ADDUSER_BADPASSWORD);
      return false
    }
    checkRightUser(e, c.val(), b.val(), f.val(), g.val(), a.val())
  }else {
    if(a.val() != d.val()) {
      alertGrowl(MISC_ERROR, USER_ADDUSER_DIFFPASSWORD);
      return false
    }
    checkRightUser(e, c.val(), b.val(), f.val(), g.val(), a.val())
  }
  c.val(""), b.val(""), a.val(""), d.val("");
  return false
}
function updateUserBase(a, b, c, d, e, f) {
    $.post(baseUrl + "/connexion/updateuser/", {login:a, email:b, pass:e, droit:c, lang:d, list:f}, alertGrowl(USER_ADDUSER_TXT, USER_ADDUSER_UPDATE), "json")
}
function addUserBase(a, b, c, d, e, f) {
  $.post(baseUrl + "/connexion/adduser/", {login:a, email:b, pass:e, droit:c, lang:d, list:f}, addUserBaseResp, "json")
}
function addUserBaseResp(a) {
  a ? alertGrowl("Cree utilisateur", "Utilisateur ajouter") : alertGrowl(USER_ADDUSER_TXT, USER_ADDUSER_EXIST)
}
function getInfoUtilisateurManager(a) {
  $.post(baseUrl + "/connexion/getinfoutilisateur/", {login:a},dialogUtilisateurManager , "json")
}
function checkRightUser(e, b, c, a, f, d) {
  if(a == "utilisateur") {
    UserRight = b;
    dialogDroitUser(e, b, c, a, f, d)
  }else {
    e ? addUserBase(b, c, a, f, d) : updateUserBase(b, c, a, f, d)
  }
}
function dialogUtilisateurManager(a) {
  var c = $("#dialogAddUser"), b = $(c + "[name|=login]"), d = $(c + "[name|=email]"), g = $(c + "[name|=right][value|=" + a.droit + "]"), f = $("#listLangage");
  b.val(a.login);
  d.val(a.mail);
  makeListLang(a);
  g.attr("checked", "checked");
  f.val(a.lang);
  d = {};
  d[BUTTON_CANCEL] = function() {
    $(this).dialog("destroy")
  };
  d[BUTTON_OK] = function() {
    $(this).dialog("destroy");
    checkDialogAddUser(false)
  };
  c.dialog({autoOpen:false, draggable:false, modal:true, width:"auto", buttons:d});
  c.dialog("open")
}
function getListGestUtilisateur() {
  $.post(baseUrl + "/connexion/getutilisateur/", {}, makeListGestUtilisateur, "json")
}
function makeListGestUtilisateur(a) {
  var b = [], c = $("#checkListUtilisateur");
  c.empty();
  for(var d = 0, e = a.list.length;d < e;d++) {
    b[d] = '<li style="color:#728C98;"><a onclick="getInfoUtilisateurManager(\''+ a.list[d] + '\')" style="cursor:pointer;">' + a.list[d] + '<a/></li>'
  }
  $(c).append(b.join(""))
}
function dialogGestionUser()
{
  getListGestUtilisateur();
  var a = $("#dialogUserList");
  var btns = {};
  btns[BUTTON_OK] = function() {$(this).dialog("destroy")};
  a.dialog({autoOpen:false, closeOnEscape: false, draggable:false, modal:true, width:200, buttons:btns});
  a.dialog("open")
}
function dialogDeconnexion() {
  var btns = {};
  btns[BUTTON_CANCEL] = function() {$(this).dialog("close")};
  btns[BUTTON_VALID] = function() {if(killUserSession() == true) {$(this).dialog("close");self.location.href = ""}};
  $("#dialogDeconnexion").dialog({autoOpen:false, draggable:false, resizable:false, width:"auto", modal:true, buttons:btns});
  $("#dialogDeconnexion").dialog("open")
}
function killUserSession() {
  $.ajax({type:"POST", url:baseUrl + "/connexion/killusersession/", success:alertGrowl(USER_TXT, USER_LOGOUT_TXT), dataType:"json", async:false});
  return true
}